package net.tv90.business.controller;

import net.tv90.business.aspect.annotation.EnumPermission;
import net.tv90.business.aspect.annotation.RequiredPermission;
import net.tv90.business.model.IdentifyInfo;
import net.tv90.business.model.dto.StudentIdentifyDTO;
import net.tv90.business.model.dto.TeacherIdentifyDTO;
import net.tv90.business.service.IdentifyService;
import net.tv90.business.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@Validated
@CrossOrigin
@RestController
@RequestMapping("/identify")
public class IdentifyController {

    @Autowired
    IdentifyService identifyService;

    @Autowired
    JmsMessagingTemplate jmsMessagingTemplate;

    @RequiredPermission(permission = EnumPermission.ALL)
    @PostMapping("/teacher")
    public Result identifyTeacher(@RequestBody @Validated TeacherIdentifyDTO dto) {
        IdentifyInfo identifyInfo = identifyService.identifyTeacher(dto);
        if (identifyInfo != null) {
            jmsMessagingTemplate.convertAndSend("queue.identify", identifyInfo);
            return Result.success(identifyInfo);
        } else {
            return Result.failed("认证失败");
        }
    }

    @RequiredPermission(permission = EnumPermission.ALL)
    @PostMapping("/student")
    public Result identifyStudent(@RequestBody @Validated StudentIdentifyDTO dto) {
        IdentifyInfo identifyInfo = identifyService.identifyStudent(dto);
        if (identifyInfo != null) {
            jmsMessagingTemplate.convertAndSend("queue.identify", identifyInfo);
            return Result.success(identifyInfo);
        } else {
            return Result.failed("认证失败");
        }
    }

    @RequiredPermission(permission = EnumPermission.ALL)
    @GetMapping("/id")
    public Result getIdentityId(String username) {
        Long id = identifyService.getId(username);
        if(id != null) {
            return Result.success(id);
        } else {
            return Result.failed("该用户名未认证");
        }
    }

}
